package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.PageResult;
import com.xique.door.bean.domain.Information;
import com.xique.door.bean.domain.SysUser;
import com.xique.door.mapper.InformationMapper;
import com.xique.door.service.IInformationService;
import com.xique.door.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information>
    implements IInformationService {

  @Autowired private ISysUserService sysUserService;

  @Override
  public PageResult getInformationList(
      Information information,
      String startTime,
      String endTime,
      String userName,
      Integer pageNum,
      Integer pageSize) {
    QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
    if (information.getInformationType() != null && !"".equals(information.getInformationType())) {
      queryWrapper.eq("information_type", information.getInformationType());
    }
    if (information.getTheme() != null && !"".equals(information.getTheme())) {
      queryWrapper.like("theme", information.getTheme());
    }
    if (information.getTitle() != null && !"".equals(information.getTitle())) {
      queryWrapper.like("title", information.getTitle());
    }
    if (startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)) {
      queryWrapper.between("release_starttime", startTime, endTime);
    }
    if (startTime != null && !"".equals(startTime)) {
      queryWrapper.ge("release_starttime", startTime);
    }
    if (userName != null && !"".equals(userName)) {
      SysUser sysUser = sysUserService.getSysUser(null, userName);
      if (sysUser != null) {
        queryWrapper.eq("update_user", sysUser.getUserId());
      } else {
        queryWrapper.isNull("update_user");
      }
    }
    queryWrapper.eq("del", 0);
    queryWrapper.orderByDesc("create_time");
    Page<Information> page = new Page<>();
    page.setCurrent(pageNum);
    page.setSize(pageSize);
    IPage<Information> list = baseMapper.selectPage(page, queryWrapper);
    if (list.getRecords().size() > 0) {
      for (Information in : list.getRecords()) {
        SysUser sysUser = sysUserService.getSysUser(in.getUpdateUser(), null);
        if (sysUser != null) {
          in.setUpdateUserName(sysUser.getUserName());
        }
      }
    }
    long totalPages = list.getPages();
    long totalSize = list.getTotal();
    return PageResult.builder()
        .pageNum(pageNum)
        .pageSize(pageSize)
        .totalPages(totalPages)
        .totalSize(totalSize)
        .list(list.getRecords())
        .build();
  }

  @Override
  public R insertInformation(Information information) {
    information.setCreateTime(new Date());
    information.setCreateUser(SecurityUtils.getUserId());
    information.setDel(0);
    information.setViews(0);
    int rep = baseMapper.insert(information);
    if (rep > 0) {
      return R.ok(null, "添加成功");
    }
    return R.fail("添加失败");
  }

  @Override
  public R updateInformation(Information information) {
    information.setUpdateTime(new Date());
    information.setUpdateUser(SecurityUtils.getUserId());
    int rep = baseMapper.updateById(information);
    if (rep > 0) {
      return R.ok(null, "修改成功");
    }
    return R.fail("修改失败");
  }

  @Override
  public R getInformation(Long id) {
    Information information = baseMapper.selectById(id);
    return R.ok(information);
  }

  @Override
  public R deleteInformation(Long id) {
    Information information = new Information();
    information.setId(id);
    information.setUpdateTime(new Date());
    information.setUpdateUser(SecurityUtils.getUserId());
    information.setDel(1);
    int rep = baseMapper.updateById(information);
    if (rep > 0) {
      return R.ok(null, "删除成功");
    }
    return R.fail("删除失败");
  }
}
